package org.springframework.study.lifecycle;

import org.springframework.context.SmartLifecycle;

/**
 * @ClassName: MySmartLifecycle
 * @Description: 无需容器显式调用start方法，就可以回调SmartLifecycle接口的start方法
 * @Author: songdongxu
 * @CreateDate: 2024/9/20 16:18
 * @Version: 1.0
 **/
public class MySmartLifecycle implements SmartLifecycle {

	private volatile boolean running = false;

	@Override
	public void start() {
		System.out.println("MySmartLifecycle 容器启动 ");
		running = true;
	}

	@Override
	public void stop() {
		System.out.println("MySmartLifecycle 容器停止 -------- 2 ");
		running = false;
	}

	@Override
	public boolean isRunning() {
		System.out.println("MySmartLifecycle 检查运行状态");
		return running;
	}

	@Override
	public boolean isAutoStartup() {
		return true;
	}

	@Override
	public void stop(Runnable callback) {
		System.out.println("MySmartLifecycle 容器停止 -------- 1 ");
		stop();
		callback.run();
	}

	@Override
	public int getPhase() {
		return 0;
	}
}
